package com.yummy.controller;


import com.github.pagehelper.PageInfo;
import com.yummy.common.BaseResponse;
import com.yummy.common.MD5Util;
import com.yummy.common.RoleConstants;
import com.yummy.common.TokenUtils;
import com.yummy.dto.QueryCategoryVo;
import com.yummy.dto.QueryEmployeeVo;
import com.yummy.entity.Category;
import com.yummy.entity.Employee;
import com.yummy.entity.Setmeal;
import com.yummy.service.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author buding
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

	@Autowired
	private CategoryService categoryService;

	@Autowired
	TokenUtils tokenUtils;

	@Autowired
	private RedisTemplate redisTemplate;


	Logger logger = LoggerFactory.getLogger(CategoryController.class);

	@PostMapping("/queryAll")
	public BaseResponse queryCategory(@RequestBody QueryCategoryVo queryCategoryVo) {
		System.out.println(queryCategoryVo);
		PageInfo<Category> pageInfo = categoryService.findAllCategory(queryCategoryVo);
		return BaseResponse.success().data("pageInfo", pageInfo);
	}

	@PostMapping("/addCategory")
	public BaseResponse addCategory(@RequestBody Category category) {

		boolean result = categoryService.addCategoryDish(category);
		if (result) {
			return BaseResponse.success();
		}
		return BaseResponse.fail();
	}

	@PostMapping("/update")
	public BaseResponse editCategory(@RequestBody Category category, HttpServletRequest request) throws NoSuchAlgorithmException {
		logger.info(category.toString());
		category.setName(category.getName());
		category.setSort(category.getSort());
		boolean result = categoryService.editCategory(category);
		if (result) {
			return BaseResponse.success().message("修改成功");
		}
		return BaseResponse.fail().message("修改失败");
	}

	@GetMapping("/remove")
	public BaseResponse removeCategory(@RequestParam String ids) {
		System.out.println("ids==>" + ids);
		boolean result = categoryService.removeCategorys(ids);
		if (result) {
			return BaseResponse.success().message("删除成功");
		} else {
			return BaseResponse.fail().message("删除失败");
		}
	}

	@GetMapping("/findCategoryById")
	public Category findCategoryById(@RequestBody String id) {
		Category category = categoryService.findCategoryById(id);
		return category;
	}


	@GetMapping("/queryCategories")
	public BaseResponse queryCategories(HttpServletRequest request) {
		Employee currentEmp = tokenUtils.getCurrentEmp(request);
		Integer storeId = currentEmp.getStoreId();
		List<Category> categories = categoryService.findCategoryByStore(storeId);
		return BaseResponse.success().data("categories", categories);
	}
}